import{p as E,C as w,E as M,T as O,G as B,m as P,s as i,H as D,K as G,j as N,d as W,I as j,c as q,o as H,w as S,a as u,t as C,u as R,f as K,h as L,g as Y}from"./DrFvzlh8agutd.js";import{a as F}from"./B15kfdAE31Z9ol.js";import{a as Q}from"./DZFz6IIX1esOcA.js";import{i as z}from"./Bb7SYMq110LR2Q.js";import{K as X,Q as Z,L as tt}from"./MpnPZlTSagutd.js";import{n as U,g as V}from"./CRB7nLEs3hcpv4.js";import{w as et}from"./WInZ5KVARz5uC.js";import{a as st,t as ot,b as rt}from"./sf48gIWk3aD96J.js";import{_ as at}from"./BfQegiIr3PfrX1.js";import{_ as nt}from"./DE1J_9T34aEgas.js";import"./2AZ41aXe85ygF.js";import"./Nc0jDSlw1t8dnT.js";/* empty css              */const it=E("rt:import");let h=1;function ct(l,o,r){return o>=l-r&&o<=l+r}function lt(l,o,r=X){const p=[],_={};for(const t of l){const s=r(t._id);_[t._id]=s,t.refR1={[t._id]:!0},t._id=s}let m=0;for(const t of l)if(it("handle item",t._id),t.kind==="receipt"||t.kind==="doc"){const s=t.currency,v=_[t.category],T=s!==o,n=w(+(t.grossOriginal??t.gross),s),y={};let k=0,x=0;if(t.taxDetails){const c=M(t.taxDetails,"taxrate desc");c.forEach(d=>{const I=w(+d.tax,s)??0,e=+d.taxRate;let a=n??0,g=n??0;c.length<=1?(g=w(a-I,s)??0,I!==0&&(ct(n??0,V(g,e),.02)||(g=w(U(I,e),s)??0))):(g=w(U(I,e),s)??0,a=w(V(g,e),s)??0);const J={category:v,gross:a,net:g,taxRate:e,sort:++h};k+=a,x+=g,y[r()]=J})}if(k!==n){const c=w((n??0)-k,s)??0,d={category:v,gross:c,net:c,taxRate:0,sort:++h};x+=c,y[r()]=d}const A=x,b=T?t.gross&&n?n/t.gross:1:void 0;t.tags=Z(t.tags)>0?Object.fromEntries(t.tags.map((c,d)=>[_[c],d])):{},t.importAssetUrl&&++m;const $={_id:t._id,_type:"doc",title:t.title,date:t.date,contact:_[t.contact],tags:t.tags,notes:t.notes,credit:!!t.credit,star:!!t.star,confirmed:!!t.confirmed,hide:!!t.trash,iban:t.iban,currency:s,gross:n,net:A,exchangeRate:b,positions:y,importAssetUrl:t.importAssetUrl};p.push($)}else if(t.kind==="category"){t.sort===null&&(t.sort=++h);const s={_id:t._id,_type:"category",title:t.title,sort:++h};p.push(s)}else if(t.kind==="tag"){t.sort===null&&(t.sort=++h);const s={_id:t._id,_type:"tag",title:t.title};p.push(s)}else if(t.kind==="contact"){t.sort===null&&(t.sort=++h);const s={_id:t._id,_type:"contact",title:t.title};p.push(s)}else throw new Error(`Unsupported kind ${t.kind}`);return[tt(p),m]}function pt(l=1e3){let o=0;return async function(){const r=performance.now();if(r-o>l)return o=r,new Promise(p=>queueMicrotask(()=>p(!0)))}}const f=E("rt:import");async function mt(l,o){const[r,p,_]=O();return st("Import from JSON",async m=>{f("start"),m.setMax(5);const t=await B(l);f("start bulk",t),m.incResolved(1);const s=t.items;if(s.some(e=>!e.kind||!e._id)){f.error("Some data is not valid"),alert("Invalid data found"),_("Import failed");return}const v=o??t.title??"Receipts",T=await et.createWorkspace(v,t.country?.toLowerCase()??"de");if(f("ws created"),await P(1),i.import=void 0,p(T.id),m.incResolved(1),!i.workspace){f.error("Workspace is not ready!");return}await z(!1),m.incResolved(1);const n=i.workspace.items[D],y=t.sCountry??n.sCountry,k=t.sCurrency??n.sCurrency,x=F[y],A=t.settings?.HOPrefRatesVAT?t.settings?.HOPrefRatesVAT?.map(e=>({rate:e,title:`${e}%`})):x?.tax?.map(e=>({rate:e,title:`${e}%`}));i.workspace.updateItem(D,{currency:k,country:y,companyName:t.settings?.SepaDebitorName,companyIban:t.settings?.SepaDebitorIBAN,sJournalViewPerYear:t.settings?.JournalViewPerYear??n.sJournalViewPerYear,numberRing:t.settings?.referenceCounter??n.sReferenceCounter,taxRates:A??[]}),await P(1);const[b,$]=lt(s,k),c=pt(),d=1/b.length;for(const e of b){const a={...e};delete a.importAssetUrl,f("create",a),i.workspace?.createItem(a),m.incResolved(d),await c()}await P(1);const I=1/$;for(const e of b)if(e.importAssetUrl){const a=await i.workspace?.sync?.nativeImportLocalAsset(e.importAssetUrl);f("assetInfo",a),a&&(a.type=e.importAssetType??"application/pdf",a.name??=`doc.${e.importAssetExtension??"pdf"}`,i.workspace?.updateItem(e._id,{asset:Q(a)})),m.incResolved(I),await c()}setTimeout(()=>{G(N("actions.import.notification.title",[v]),N("actions.import.notification.message"))}),f("end bulk")}),r}const dt={class:"_center"},ut=["max","value"],Tt=W({__name:"app-page-import1",setup(l){return j(async()=>{i.import?.url?mt(i.import?.url.replace("file:///","r2import:///"),i.import?.title??N("unnamed")):i.import=void 0}),(o,r)=>(H(),q(nt,null,{default:S(()=>[u("div",dt,[u("h1",null,C(o.$t("import1.title")),1),u("p",null,[u("b",null,'"'+C(R(i).import?.title??o.$t("unnamed"))+'"',1)]),u("p",null,C(o.$t("import1.description")),1),u("p",null,[u("progress",{max:R(rt),value:R(ot)},null,8,ut)]),u("p",null,[K(R(at),{disabled:R(i).import!=null,onClick:r[0]||(r[0]=p=>R(L).workspacePage())},{default:S(()=>[Y(C(o.$t("import1.buttonCreate")),1)]),_:1},8,["disabled"])])])]),_:1}))}});export{Tt as default};